//
//  CYBaseView.swift
//  MySina
//
//  Created by mac on 2017/1/17.
//  Copyright © 2017年 xinyuan. All rights reserved.
//

import UIKit

class CYBaseView: UIView {

    var closure: (() -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupBaseViewInfo(imgName: String?, title: String?) {
        if let img = imgName, let tit = title {
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            //隐藏圈圈图片
            feedImageView.isHidden = true
        } else {
            setupFeedImageViewAnimation()
        }
        
    }
    
    //设置圆圈动画
    func setupFeedImageViewAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        //运行完不移除动画
        anim.isRemovedOnCompletion = false
        feedImageView.layer.add(anim, forKey: nil)
    }
    
    private func setupUI() {
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        //添加约束
        feedImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        messageLabel.snp_makeConstraints { (make) in
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.width.equalTo(230)
            make.centerX.equalTo(self)
        }
        loginButton.snp_makeConstraints { (make) in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        registerButton.snp_makeConstraints { (make) in
            make.top.equalTo(loginButton)
            make.size.equalTo(loginButton)
            make.right.equalTo(messageLabel)
        }
    }
    
    //懒加载空间
    //圈圈
    private lazy var feedImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    private lazy var maskImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    private lazy var iconImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    //label设置
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14.0)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGray
        label.numberOfLines = 0
        label.textAlignment = .center
        return label
    }()
    
//    登录按钮设置
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(ButtonClick), for: UIControlEvents.touchUpInside)
        btn.setBackgroundImage(UIImage(named: "common_button_white"), for: UIControlState.normal)
        btn.setTitle("登录", for: UIControlState.normal)
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.setTitleColor(OrangeColor, for: UIControlState.normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        return btn
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(ButtonClick), for: UIControlEvents.touchUpInside)
        btn.setBackgroundImage(UIImage(named: "common_button_white"), for: UIControlState.normal)
        btn.setTitle("注册", for: UIControlState.normal)
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.setTitleColor(OrangeColor, for: UIControlState.normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14) 
        return btn
    }()
    
    @objc func ButtonClick() {
         closure?()
    }
}

extension CYBaseView {
    //        feedImageView.translatesAutoresizingMaskIntoConstraints = false
    //        addConstraint(NSLayoutConstraint(item: feedImageView, attribute: NSLayoutAttribute.centerX, relatedBy: <#T##NSLayoutRelation#>, toItem: <#T##Any?#>, attribute: <#T##NSLayoutAttribute#>, multiplier: <#T##CGFloat#>, constant: <#T##CGFloat#>))
    //        addConstraint(NSLayoutConstraint(item: <#T##Any#>, attribute: <#T##NSLayoutAttribute#>, relatedBy: <#T##NSLayoutRelation#>, toItem: <#T##Any?#>, attribute: <#T##NSLayoutAttribute#>, multiplier: <#T##CGFloat#>, constant: <#T##CGFloat#>))
}









